package com.example.score.service;

import com.example.score.entity.*;
import com.example.score.entity.vo.GradeVo;
import com.example.score.entity.vo.TUserVo;
import com.example.score.mapper.GradeMapper;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class GradeService {
    @Resource
    private UserService userService;
    @Resource
    private GradeMapper gradeMapper;
    @Resource
    private CourseService courseService;
    @Transactional
    public List<TotalGradeDetail> getTotalGrade(Integer classId) {
        Scale scale = courseService.selectScale(classId);
        //教师评分占比
        double teacherScale = scale.getTeascale();
        System.out.println(teacherScale);
        //学生评分占比
        double studentScale = scale.getStuscale();
        System.out.println(studentScale);
        //成绩明细
        List<TotalGradeDetail> detailsList = gradeMapper.getAllGradeDetails(classId);
        //教师汇总成绩
        List<TotalGrade> teacherList = gradeMapper.getTotalOfTeacher(classId);
        //学生汇总成绩
        List<TotalGrade> studentList = gradeMapper.getTotalOfStudent(classId);
        int typf = gradeMapper.getTYPF(classId);
        int sypf = gradeMapper.getSYPF(classId);
        int pfrs = gradeMapper.getpfrs(classId) - 1;
        //班级所有学生汇总成绩
//        List<TotalGradeDetail> teacherGradesList = new ArrayList<TotalGradeDetail>();
//        List<TotalGradeDetail> studentGradesList = new ArrayList<TotalGradeDetail>();
        List<TotalGradeDetail> gradesList = new ArrayList<TotalGradeDetail>();
        int index=0;



        Map<Integer, TotalGradeDetail> tSumMap = new HashMap<>();

        for (int i = 0; i < teacherList.size(); i++) {
            TotalGradeDetail newGrade = new TotalGradeDetail();
            TotalGrade teacherGrade = teacherList.get(i);
            TUser dbr = userService.getUserById(teacherGrade.getDbr());
            String username = dbr.getUsername();
            String chname = dbr.getChname();

            if (tSumMap.containsKey(dbr.getId())) {
                TotalGradeDetail sumGrade = tSumMap.get(dbr.getId());
                sumGrade.setPfbzs(scale.getPfbzs());
                // 对每个分组的数据进行累加
                sumGrade.setPf1(sumGrade.getPf1() + teacherGrade.getTotalpf1() * teacherScale);
                sumGrade.setPf2(sumGrade.getPf2() + teacherGrade.getTotalpf2() * teacherScale);
                sumGrade.setPf3(sumGrade.getPf3() + teacherGrade.getTotalpf3() * teacherScale);
                sumGrade.setPf4(sumGrade.getPf4() + teacherGrade.getTotalpf4() * teacherScale);
                sumGrade.setPf5(sumGrade.getPf5() + teacherGrade.getTotalpf5() * teacherScale);
                sumGrade.setZf(sumGrade.getZf() + teacherGrade.getTotalGrade() * teacherScale);
                sumGrade.setState(sumGrade.getState()+teacherGrade.getState() / pfrs);
                tSumMap.put(dbr.getId(), sumGrade);
            } else {
                TotalGradeDetail sumGrade = new TotalGradeDetail();
                sumGrade.setDbr(dbr.getId());
                sumGrade.setDbrUsernam(username);
                sumGrade.setDbrChname(chname);
                sumGrade.setPfbzs(scale.getPfbzs());
                sumGrade.setPf1(teacherGrade.getTotalpf1() * teacherScale);
                sumGrade.setPf2(teacherGrade.getTotalpf2() * teacherScale);
                sumGrade.setPf3(teacherGrade.getTotalpf3() * teacherScale);
                sumGrade.setPf4(teacherGrade.getTotalpf4() * teacherScale);
                sumGrade.setPf5(teacherGrade.getTotalpf5() * teacherScale);
                sumGrade.setZf(teacherGrade.getTotalGrade() * teacherScale);
                sumGrade.setState(sumGrade.getState()+teacherGrade.getState() / pfrs);
                tSumMap.put(dbr.getId(), sumGrade);
            }
        }
        if(typf !=0 ){
            for (TotalGradeDetail sumGrade : tSumMap.values()) {
                sumGrade.setPf1(sumGrade.getPf1() / typf);
                sumGrade.setPf2(sumGrade.getPf2() / typf);
                sumGrade.setPf3(sumGrade.getPf3() / typf);
                sumGrade.setPf4(sumGrade.getPf4() / typf);
                sumGrade.setPf5(sumGrade.getPf5() / typf);
                sumGrade.setZf(sumGrade.getZf() / typf);
            }}

        Map<Integer, TotalGradeDetail> sSumMap = new HashMap<>();
        for (int i = 0; i < studentList.size(); i++) {
            TotalGradeDetail newGrade = new TotalGradeDetail();
            TotalGrade studentGrade = studentList.get(i);
            TUser dbr = userService.getUserById(studentGrade.getDbr());
            String username = dbr.getUsername();
            String chname = dbr.getChname();

            if (sSumMap.containsKey(dbr.getId())) {
                TotalGradeDetail sumGrade = sSumMap.get(dbr.getId());
                sumGrade.setPfbzs(scale.getPfbzs());
                // 对每个分组的数据进行累加
                sumGrade.setPf1(sumGrade.getPf1() + studentGrade.getTotalpf1() * studentScale);
                sumGrade.setPf2(sumGrade.getPf2() + studentGrade.getTotalpf2() * studentScale);
                sumGrade.setPf3(sumGrade.getPf3() + studentGrade.getTotalpf3() * studentScale);
                sumGrade.setPf4(sumGrade.getPf4() + studentGrade.getTotalpf4() * studentScale);
                sumGrade.setPf5(sumGrade.getPf5() + studentGrade.getTotalpf5() * studentScale);
                sumGrade.setZf(sumGrade.getZf() + studentGrade.getTotalGrade() * studentScale);
                sumGrade.setState(sumGrade.getState()+studentGrade.getState() / pfrs);
                sSumMap.put(dbr.getId(), sumGrade);
            } else {
                TotalGradeDetail sumGrade = new TotalGradeDetail();
                sumGrade.setDbr(dbr.getId());
                sumGrade.setDbrUsernam(username);
                sumGrade.setDbrChname(chname);
                sumGrade.setPfbzs(scale.getPfbzs());
                sumGrade.setPf1(studentGrade.getTotalpf1() * studentScale);
                sumGrade.setPf2(studentGrade.getTotalpf2() * studentScale);
                sumGrade.setPf3(studentGrade.getTotalpf3() * studentScale);
                sumGrade.setPf4(studentGrade.getTotalpf4() * studentScale);
                sumGrade.setPf5(studentGrade.getTotalpf5() * studentScale);
                sumGrade.setZf(studentGrade.getTotalGrade() * studentScale);
                sumGrade.setState(sumGrade.getState()+studentGrade.getState() / pfrs);
                sSumMap.put(dbr.getId(), sumGrade);
            }
        }

        if (sypf != 0) {
            for (TotalGradeDetail sumGrade2 : sSumMap.values()) {
                sumGrade2.setPf1(sumGrade2.getPf1() / sypf);
                sumGrade2.setPf2(sumGrade2.getPf2() / sypf);
                sumGrade2.setPf3(sumGrade2.getPf3() / sypf);
                sumGrade2.setPf4(sumGrade2.getPf4() / sypf);
                sumGrade2.setPf5(sumGrade2.getPf5() / sypf);
                sumGrade2.setZf(sumGrade2.getZf() / sypf);
            }
        }

        List<TotalGradeDetail> teacherSummedGradesList = new ArrayList<>(tSumMap.values());
        List<TotalGradeDetail> studentSummedGradesList = new ArrayList<>(sSumMap.values());
        List<TotalGradeDetail> combinedGradesList = new ArrayList<>();

// 假设teacherSummedGradesList和studentSummedGradesList长度相同
        for (int i = 0; i < teacherSummedGradesList.size(); i++) {
            // 创建一个新的TotalGradeDetail对象，用于存储相同索引位置的对象字段相加后的结果
            TotalGradeDetail combinedGrade = new TotalGradeDetail();

            // 获取相同索引位置的老师和学生的对象
            TotalGradeDetail teacherGrade = teacherSummedGradesList.get(i);
            TotalGradeDetail studentGrade = studentSummedGradesList.get(i);

            // 将相应字段相加，并存储在combinedGrade对象中
            combinedGrade.setDbr(teacherGrade.getDbr());
            combinedGrade.setDbrUsernam(teacherGrade.getDbrUsernam());
            combinedGrade.setDbrChname(teacherGrade.getDbrChname());
            combinedGrade.setPfbzs(scale.getPfbzs());
            combinedGrade.setPfbzs(teacherGrade.getPfbzs());
            combinedGrade.setPf1(Math.round(teacherGrade.getPf1() + studentGrade.getPf1()));
            combinedGrade.setPf2(Math.round(teacherGrade.getPf2() + studentGrade.getPf2()));
            combinedGrade.setPf3(Math.round(teacherGrade.getPf3() + studentGrade.getPf3()));
            combinedGrade.setPf4(Math.round(teacherGrade.getPf4() + studentGrade.getPf4()));
            combinedGrade.setPf5(Math.round(teacherGrade.getPf5() + studentGrade.getPf5()));
            combinedGrade.setZf(Math.round(teacherGrade.getZf() + studentGrade.getZf()));
            combinedGrade.setState(Math.round(teacherGrade.getState() + studentGrade.getState()));
            combinedGradesList.add(combinedGrade);
        }
        gradesList.addAll(combinedGradesList);
        return gradesList;
    }

    public List<TotalGradeDetail> getAllGradeDetails(Integer classId) {
        return gradeMapper.getAllGradeDetails(classId);
    }
    public List<TUser> unargument(Integer dbr) {
        return gradeMapper.unargument(dbr);
    }
    public List<GradeVo> argument(Integer dbr) {
        return gradeMapper.argument(dbr);
    }

    public List<GradeStudentList> viewed(Map<String, Integer> map) {
        return gradeMapper.viewed(map);
    }
    public int updateAnswerGrade(Grade grade) {
        return gradeMapper.updateAnswerGrade(grade);
    }

    public List<Course> pfbzmsg(Integer classId) {
        return gradeMapper.pfbzmsg(classId);
    }

    public List<TotalGradeDetail> showAll(Integer classId){
        Scale scale = courseService.selectScale(classId);
        //教师评分占比
        double teacherScale = scale.getTeascale();
        System.out.println(teacherScale);
        //学生评分占比
        double studentScale = scale.getStuscale();
        System.out.println(studentScale);
        //成绩明细
        List<TotalGradeDetail> detailsList = gradeMapper.getAllGradeDetails(classId);
        //教师汇总成绩
        List<TotalGrade> teacherList = gradeMapper.getTotalOfTeacher(classId);
        //学生汇总成绩
        List<TotalGrade> studentList = gradeMapper.getTotalOfStudent(classId);
        int typf = gradeMapper.getTYPF(classId);
        int sypf = gradeMapper.getSYPF(classId);
        int pfrs = gradeMapper.getpfrs(classId) - 1;
        //班级所有学生汇总成绩
//        List<TotalGradeDetail> teacherGradesList = new ArrayList<TotalGradeDetail>();
//        List<TotalGradeDetail> studentGradesList = new ArrayList<TotalGradeDetail>();
        List<TotalGradeDetail> gradesList = new ArrayList<TotalGradeDetail>();
        int index=0;



        Map<Integer, TotalGradeDetail> tSumMap = new HashMap<>();

        for (int i = 0; i < teacherList.size(); i++) {
            TotalGradeDetail newGrade = new TotalGradeDetail();
            TotalGrade teacherGrade = teacherList.get(i);
            TUser dbr = userService.getUserById(teacherGrade.getDbr());
            String username = dbr.getUsername();
            String chname = dbr.getChname();

            if (tSumMap.containsKey(dbr.getId())) {
                TotalGradeDetail sumGrade = tSumMap.get(dbr.getId());
                sumGrade.setPfbzs(scale.getPfbzs());
                // 对每个分组的数据进行累加
                sumGrade.setPf1(sumGrade.getPf1() + teacherGrade.getTotalpf1() * teacherScale);
                sumGrade.setPf2(sumGrade.getPf2() + teacherGrade.getTotalpf2() * teacherScale);
                sumGrade.setPf3(sumGrade.getPf3() + teacherGrade.getTotalpf3() * teacherScale);
                sumGrade.setPf4(sumGrade.getPf4() + teacherGrade.getTotalpf4() * teacherScale);
                sumGrade.setPf5(sumGrade.getPf5() + teacherGrade.getTotalpf5() * teacherScale);
                sumGrade.setZf(sumGrade.getZf() + teacherGrade.getTotalGrade() * teacherScale);
                sumGrade.setState(sumGrade.getState()+teacherGrade.getState() / pfrs);
                tSumMap.put(dbr.getId(), sumGrade);
            } else {
                TotalGradeDetail sumGrade = new TotalGradeDetail();
                sumGrade.setDbr(dbr.getId());
                sumGrade.setDbrUsernam(username);
                sumGrade.setDbrChname(chname);
                sumGrade.setPfbzs(scale.getPfbzs());
                sumGrade.setPf1(teacherGrade.getTotalpf1() * teacherScale);
                sumGrade.setPf2(teacherGrade.getTotalpf2() * teacherScale);
                sumGrade.setPf3(teacherGrade.getTotalpf3() * teacherScale);
                sumGrade.setPf4(teacherGrade.getTotalpf4() * teacherScale);
                sumGrade.setPf5(teacherGrade.getTotalpf5() * teacherScale);
                sumGrade.setZf(teacherGrade.getTotalGrade() * teacherScale);
                sumGrade.setState(sumGrade.getState()+teacherGrade.getState() / pfrs);
                tSumMap.put(dbr.getId(), sumGrade);
            }
        }
        if(typf !=0 ){
        for (TotalGradeDetail sumGrade : tSumMap.values()) {
            sumGrade.setPf1(sumGrade.getPf1() / typf);
            sumGrade.setPf2(sumGrade.getPf2() / typf);
            sumGrade.setPf3(sumGrade.getPf3() / typf);
            sumGrade.setPf4(sumGrade.getPf4() / typf);
            sumGrade.setPf5(sumGrade.getPf5() / typf);
            sumGrade.setZf(sumGrade.getZf() / typf);
        }}

        Map<Integer, TotalGradeDetail> sSumMap = new HashMap<>();
        for (int i = 0; i < studentList.size(); i++) {
            TotalGradeDetail newGrade = new TotalGradeDetail();
            TotalGrade studentGrade = studentList.get(i);
            TUser dbr = userService.getUserById(studentGrade.getDbr());
            String username = dbr.getUsername();
            String chname = dbr.getChname();

            if (sSumMap.containsKey(dbr.getId())) {
                TotalGradeDetail sumGrade = sSumMap.get(dbr.getId());
                sumGrade.setPfbzs(scale.getPfbzs());
                // 对每个分组的数据进行累加
                sumGrade.setPf1(sumGrade.getPf1() + studentGrade.getTotalpf1() * studentScale);
                sumGrade.setPf2(sumGrade.getPf2() + studentGrade.getTotalpf2() * studentScale);
                sumGrade.setPf3(sumGrade.getPf3() + studentGrade.getTotalpf3() * studentScale);
                sumGrade.setPf4(sumGrade.getPf4() + studentGrade.getTotalpf4() * studentScale);
                sumGrade.setPf5(sumGrade.getPf5() + studentGrade.getTotalpf5() * studentScale);
                sumGrade.setZf(sumGrade.getZf() + studentGrade.getTotalGrade() * studentScale);
                sumGrade.setState(sumGrade.getState()+studentGrade.getState() / pfrs);
                sSumMap.put(dbr.getId(), sumGrade);
            } else {
                TotalGradeDetail sumGrade = new TotalGradeDetail();
                sumGrade.setDbr(dbr.getId());
                sumGrade.setDbrUsernam(username);
                sumGrade.setDbrChname(chname);
                sumGrade.setPfbzs(scale.getPfbzs());
                sumGrade.setPf1(studentGrade.getTotalpf1() * studentScale);
                sumGrade.setPf2(studentGrade.getTotalpf2() * studentScale);
                sumGrade.setPf3(studentGrade.getTotalpf3() * studentScale);
                sumGrade.setPf4(studentGrade.getTotalpf4() * studentScale);
                sumGrade.setPf5(studentGrade.getTotalpf5() * studentScale);
                sumGrade.setZf(studentGrade.getTotalGrade() * studentScale);
                sumGrade.setState(sumGrade.getState()+studentGrade.getState() / pfrs);
                sSumMap.put(dbr.getId(), sumGrade);
            }
        }

        if (sypf != 0) {
            for (TotalGradeDetail sumGrade2 : sSumMap.values()) {
                sumGrade2.setPf1(sumGrade2.getPf1() / sypf);
                sumGrade2.setPf2(sumGrade2.getPf2() / sypf);
                sumGrade2.setPf3(sumGrade2.getPf3() / sypf);
                sumGrade2.setPf4(sumGrade2.getPf4() / sypf);
                sumGrade2.setPf5(sumGrade2.getPf5() / sypf);
                sumGrade2.setZf(sumGrade2.getZf() / sypf);
            }
        }

        List<TotalGradeDetail> teacherSummedGradesList = new ArrayList<>(tSumMap.values());
        List<TotalGradeDetail> studentSummedGradesList = new ArrayList<>(sSumMap.values());
        List<TotalGradeDetail> combinedGradesList = new ArrayList<>();

// 假设teacherSummedGradesList和studentSummedGradesList长度相同
        for (int i = 0; i < teacherSummedGradesList.size(); i++) {
            // 创建一个新的TotalGradeDetail对象，用于存储相同索引位置的对象字段相加后的结果
            TotalGradeDetail combinedGrade = new TotalGradeDetail();

            // 获取相同索引位置的老师和学生的对象
            TotalGradeDetail teacherGrade = teacherSummedGradesList.get(i);
            TotalGradeDetail studentGrade = studentSummedGradesList.get(i);

            // 将相应字段相加，并存储在combinedGrade对象中
            combinedGrade.setDbr(teacherGrade.getDbr());
            combinedGrade.setDbrUsernam(teacherGrade.getDbrUsernam());
            combinedGrade.setDbrChname(teacherGrade.getDbrChname());
            combinedGrade.setPfbzs(scale.getPfbzs());
            combinedGrade.setPfbzs(teacherGrade.getPfbzs());
            combinedGrade.setPf1(Math.round(teacherGrade.getPf1() + studentGrade.getPf1()));
            combinedGrade.setPf2(Math.round(teacherGrade.getPf2() + studentGrade.getPf2()));
            combinedGrade.setPf3(Math.round(teacherGrade.getPf3() + studentGrade.getPf3()));
            combinedGrade.setPf4(Math.round(teacherGrade.getPf4() + studentGrade.getPf4()));
            combinedGrade.setPf5(Math.round(teacherGrade.getPf5() + studentGrade.getPf5()));
            combinedGrade.setZf(Math.round(teacherGrade.getZf() + studentGrade.getZf()));
            combinedGrade.setState(Math.round(teacherGrade.getState() + studentGrade.getState()));
            combinedGradesList.add(combinedGrade);
        }
        gradesList.addAll(combinedGradesList);
        return gradesList;
    }

    public int updateScore(UpdateScore updateScore) {
        return gradeMapper.updateScroe(updateScore);
    }
    @Transactional
    public RandomStudent getRandomStudent(Integer classId) {
        // 添加 @Transactional 注解来启用事务管理
        gradeMapper.updateStatesTo2(classId);  // 调用第一个Mapper方法
        gradeMapper.updateRandomStudentState(classId);  // 调用第二个Mapper方法
        gradeMapper.selectRandomStudents(classId);  // 调用第三
        return gradeMapper.selectRandomStudents(classId);
    }

    public RandomStudent talking(Integer classId) {
       return gradeMapper.selectRandomStudents(classId);
    }

    public void endTalkingStudent(Integer classId) {
        gradeMapper.updateStatesTo2(classId);// 调用第一个Mapper方法
    }
    public void over(Integer classId) {
        gradeMapper.over(classId);// 调用第一个Mapper方法
    }
}
